import { _decorator, Component, instantiate, Label, Node } from 'cc';
import { GameData } from '../system/GameData';
import { DataHelper } from '../system/DataHelper';
import { AppControll, ConfigKey, configKey, skin_config } from '../manager/AppControll';
import { SkinItem } from './SkinItem';
import { SoundManager } from '../manager/SoundManger';
const { ccclass, property } = _decorator;

export enum unlockType {
    MoreDamage = 1,
    MoreGold = 2,
    AttackInterval = 3
}

@ccclass('Skin')
export class Skin extends Component {
    @property(Node)
    mNdContent: Node = null;
    @property(Label)
    mLbEffectDescription: Label = null;
    @property(Node)
    mItem: Node = null;
    @property(Node)
    mNdNoSkin: Node = null

    protected onLoad(): void {
        GameData.isPause = true;
        this.InitSkin();
        this.InitItem();
    }

    InitSkin() {
        let arr = GameData.playerData.skin;
        if (arr.length == 1) {
            this.mLbEffectDescription.string = "";
            this.mNdNoSkin.active = true;
        } else {
            let temp1 = 0;
            let temp2 = 0;
            let temp3 = 0;
            let str = ""
            arr.forEach(id => {
                let config = DataHelper.GetSkin_configById(id);
                switch (config.unlockAttrType) {
                    case unlockType.MoreDamage:
                        temp1 += config.unlockNum;
                        break;
                    case unlockType.MoreGold:
                        temp2 += config.unlockNum;
                        break;
                    case unlockType.AttackInterval:
                        temp3 += config.unlockNum;
                        break;
                }
            })
            let value0 = ""
            if (temp1 > 0) {
                value0 = (temp1 * 100).toString();
                let tempstr1 = DataHelper.GetConfigByKey(ConfigKey.AtkAddText).value;
                tempstr1 = tempstr1.replace('${value0}', value0);
                str += tempstr1;
            }
            if (temp2 > 0) {
                value0 = (temp2 * 100).toString();
                let tempstr2 = DataHelper.GetConfigByKey(ConfigKey.goldAddText).value;
                tempstr2 = tempstr2.replace('${value0}', value0);
                if (str != "") str += ", "
                str += tempstr2;
            }
            if (temp3 > 0) {
                value0 = (temp3).toString();
                let tempstr3 = DataHelper.GetConfigByKey(ConfigKey.autoAtkTimesText).value;
                tempstr3 = tempstr3.replace('${value0}', value0);
                if (str != "") str += ", "
                str += tempstr3;
            }
            this.mLbEffectDescription.string = str;
            this.mNdNoSkin.active = false;
        }
    }

    InitItem() {
        let skin = DataHelper.GetSkin_config();
        for (const key in skin) {
            if (Object.prototype.hasOwnProperty.call(skin, key)) {
                const config = skin[key];
                let node = instantiate(this.mItem);
                node.active = true;
                node.getComponent(SkinItem).InitSkinItemData(config);
                this.mNdContent.addChild(node);
            }
        }
    }

    start() {

    }

    update(deltaTime: number) {

    }

    OnClose() {
        GameData.isPause = false;
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick)
        this.mNdContent.destroyAllChildren()
        this.node.destroy();
    }

    OnScrollView() {
        AppControll.ShelvedTime = 0;
    }
}


